$(function () {
  $('#link_reg').on('click', function () {
    $('.login-box').hide()
    $('.reg-box').show()
  })
  $('#link_login').on('click', function () {
    $('.login-box').show()
    $('.reg-box').hide()
  })

  var form = layui.form;
  form.verify({
    pwd: [
      /^[\S]{6,12}$/
      , '密码必须6到12位，且不能出现空格'
    ],
    repwd: function (value) {
      var pwd = $('.reg-box [name=password]').val()
      if (pwd !== value) return '两次密码不一致'
    }
  })

  $('#form_reg').on('submit', function (e) {
    e.preventDefault();
    $.ajax({
      method: 'POST',
      url: '/api/reguser',
      data: $(this).serialize(),
      success: function (res) {
        layui.layer.msg(res.message)
        if (res.status !== 0) return
        $('#link_login').click();
      }
    })
  })

  $('#form_login').on('submit', function (e) {
    e.preventDefault()
    $.ajax({
      method: 'POST',
      url: '/api/login',
      data: $(this).serialize(),
      success: function (res) {
        layui.layer.msg(res.message)
        if (res.status == 0) {
          localStorage.setItem('token', res.token)
          location.href = 'index.html'
        }
      }
    })
  })
})